<?php

	require_once('Controller.php');
	require_once('model/imageDAO.php');
	/**
	* Photo, Affichage de photo unique
	*/
	class Photo extends Controller
	{
		protected $imgDAO;
		protected $action;
		
		public function __construct ()
		{
			parent::__construct();
			if (!isset($_GET['size']))
				$this->data->size = 480;
			else
				$this->data->size = $_GET['size'];
			if (!isset($_GET['categorie']))
				$_GET['categorie'] = "";
			
			if ($_GET['action'] != "add")
			{
				$this->data->menu['First'] = '?controller=Photo&action=first';
				$this->data->menu['Random'] = '?controller=Photo&action=random';
				if (isset($_GET['imgId'])) {
					$this->data->menu['More'] = '?controller=PhotoMatrix&imgId='.$_GET["imgId"];
				} else {
					$this->data->menu['More'] = '?controller=PhotoMatrix';
				}
				$this->data->menu['Zoom +'] = '?controller=Photo&action=zoom&zoom=1.25&imgId=';
				$this->data->menu['Zoom -'] = '?controller=Photo&action=zoom&zoom=0.8&imgId=';
			}
			$this->data->menu['Voir les photos'] = '?controller=Photo&action=index';
			$this->data->menu['Voir les albums'] = '?controller=Album&action=index';
			$this->imgDAO = new ImageDAO();	
		}
		
		public function __destruct ()
		{
			if ($_GET['action'] != 'add')
			{
				$this->data->menu['Zoom +'] .= $this->data->image->getId().'&size='.$this->data->size;
				$this->data->menu['Zoom -'] .= $this->data->image->getId().'&size='.$this->data->size;
				if( !isset($_GET['imgId']) ) {
					$this->data->menu['More'] .= "&imgId=".$this->data->image->getId();
				}
				$current_categorie 					= ( isset($_GET["categorie"]) ) ? $_GET["categorie"] : null;
				$this->data->current_categorie 		= $current_categorie;
				$this->data->previousId 			= $this->imgDAO->getPreviousId($this->data->image->getId(), $current_categorie);
				$this->data->nextId 				= $this->imgDAO->getNextId($this->data->image->getId(), $current_categorie);
			}
			
			$this->data->categories 			= $this->imgDAO->getCategories();
			
			if ($this->action == "edit")
				$this->data->view = 'edit.php';
			elseif ($this->action == "add")
				$this->data->view = 'add.php';
			elseif ($this->action == 'error')
				$this->data->view = 'error.php';
			else
				$this->data->view = 'photoView.php';
			parent::__destruct();
		}
		
		public function index ()
		{
			$this->first();
		}
	
		public function first ()
		{
			if( isset($_GET["categorie"]) && !empty($_GET["categorie"]) )
				$this->data->image = $this->imgDAO->getImageByCat($_GET["categorie"]);
			else
				$this->data->image = $this->imgDAO->getFirstImage();
		}
		
		public function next ()
		{
			if (!isset($_GET['imgId'])) {
				$this->first();
			} else {
				$this->data->image = $this->imgDAO->getNextImage($_GET['imgId'], $_GET['categorie']);
			}
		}
		
		public function prev ()
		{
			if (!isset($_GET['imgId'])) {
				$this->first();
			} else {
				$this->data->image = $this->imgDAO->getPrevImage($_GET['imgId']);
			}
		}
		
		public function random ()
		{
			$this->data->image = $this->imgDAO->getRandomImage();
		}
		
		public function zoom ()
		{
			if (isset($_GET['zoom']))
				$this->data->size *= $_GET['zoom'];
			$this->data->image = $this->imgDAO->getImage($_GET['imgId']);
		}
		
		public function edit ()
		{
			if( !isset($_GET['imgId']) )
				$_GET['imgId'] = 1;
			if( !isset($_GET['categorie']) )
				$_GET['categorie'] = null;
			$this->data->image = $this->imgDAO->getImage($_GET['imgId']);
			$this->data->categories = $this->imgDAO->getCategories();
			$this->action = 'edit';
			if (!empty($_POST)) {
				$this->data->image->setCategory($_POST['category']);
				$this->data->image->setComment($_POST['comment']);
				$this->imgDAO->save($this->data->image);
				header('Location: ?controller=Photo&action=next&imgId='.($this->data->image->getId()).'&categorie='.$_GET['categorie']);
			}
		}

		public function add ()
		{
			$this->action = "add";
			// S'il n'y a pas d'erreur sur le fichier et qu'il fait moins de 510Ko
			if( empty($_POST["url_photo"]) ) {
				if(!empty($_FILES['image']) && $_FILES['image']['size'] < 510000 && $_FILES['image']['error'] == 0) {
					$picture_temp = $_FILES['image']['tmp_name'];
					$picture = $_FILES['image']['name'];
					$nom = md5(uniqid(rand(), true));
					$img = new Image("", null, $_POST['comment'], $_POST['category'], 'uploaded/'.$nom.'-'.$picture, 0, 0);
					$save = $this->imgDAO->save($img);
					move_uploaded_file($picture_temp, $_SERVER['DOCUMENT_ROOT'].'/projet/IMG/uploaded/'.$nom.'-'.$picture);
					header('Location:index.php?controller=Photo&action=next&imgId='.$this->imgDAO->getLastInsertId().'&categorie='.$img->getCategory());
				}
			} else {
				$img = new Image("", null, $_POST['comment'], $_POST['category'], $_POST['url_photo'], 0, 0);
				$save = $this->imgDAO->save($img);
			}
		}
		
		public function note()
		{
			if (!isset($_GET['imgId']))
				$_GET['imgId'] = 1;
			$this->data->image = $this->imgDAO->getImage($_GET['imgId']);
			if (!isset($_GET['note']))
			{
				$this->action = 'error';
				$this->__destruct();
			}
			else
			{
				if ($_GET['note'] == "like")
				{
					$this->data->image->like();
					$this->imgDAO->save($this->data->image);
				}
				elseif ($_GET['note'] == "unlike")
				{
					$this->data->image->unlike();
					$this->imgDAO->save($this->data->image);
				}
				else
				{
					$this->action = 'error';
					$this->data->error = 'L\'action n\'existe pas';
					$this->__destruct();
				}
			}
		}
	
	}
